<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Message extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(2);
		$this->load->model('Message_model', 'message');
        $this->lang->load('company', $this->session->userdata['user_lang']);
	}
	
	public function search()
	{
		$data = $_POST;
		$data['limit'] = 5;
		$this->lang->load('message', $this->session->userdata['user_lang']);
		$message_list = $this->message->get_member_messages($data);
		
		$grid_messages = array();
		
        foreach ($message_list->result() as $row)
		{
			$message_link = $this->get_member_link($row->idmemberfrom, $row->idmemberfromtype, true, $row->idsubscription);
			$memberfrom_link = $this->get_member_link($row->idmemberfrom, $row->idmemberfromtype, false, $row->idsubscription);
			$memberto_link = $this->get_member_link($row->idmemberto, $row->idmembertotype, false, $row->idsubscription);
			$job_link = $this->get_member_link($row->idsubscription, 4);
			
			switch ($row->idtype)
			{
				case 1:
					$message_content = $message_link . '<div class="message-content"><div class="message-date">' . sprintf(format_date($row->date, true, true, true), $this->lang->line('global_in'), $this->lang->line('global_at')) . '</div>' . sprintf($this->lang->line('message_subscriptionfrom'), $memberfrom_link, $job_link) . '</div>'; 
					break;
				case 4:
					if ($row->idmemberfrom == $this->session->userdata['idmember'])
					{
						$message_content = $message_link . '<div class="message-content"><div class="message-date">' . sprintf(format_date($row->date, true, true, true), $this->lang->line('global_in'), $this->lang->line('global_at')) . '</div>' . sprintf($this->lang->line('message_subscriptionchatfrom'), $memberto_link, $job_link) . ':<br/><br/><p>'.$row->message.'</p></div>';
					}
					else
					{
						$message_content = $message_link . '<div class="message-content"><div class="message-date">' . sprintf(format_date($row->date, true, true, true), $this->lang->line('global_in'), $this->lang->line('global_at')) . '</div>' . sprintf($this->lang->line('message_subscriptionchatto'), $memberfrom_link, $job_link) . ':<br/><br/><p>'.$row->message.'</p></div>';
					}
					break;
				case 5:
					if ($row->idmemberfrom == $this->session->userdata['idmember'])
					{
						$message_content = $message_link . '<div class="message-content"><div class="message-date">' . sprintf(format_date($row->date, true, true, true), $this->lang->line('global_in'), $this->lang->line('global_at')) . '</div>' . sprintf($this->lang->line('message_interviewinvitefrom'), $memberto_link, $job_link) . ':<br/><br/><p><a href="'.base_url().'interview/enter/'.$row->idinterview.'" target="_blank">'.$this->lang->line('message_accessinterview').'</a></p></div>';
					}
					else
					{
						$message_content = $message_link . '<div class="message-content"><div class="message-date">' . sprintf(format_date($row->date, true, true, true), $this->lang->line('global_in'), $this->lang->line('global_at')) . '</div>' . sprintf($this->lang->line('message_interviewinviteto'), $memberfrom_link, $job_link) . ':<br/><br/><p><a href="'.base_url().'interview/enter/'.$row->idinterview.'" target="_blank">'.$this->lang->line('message_accessinterview').'</a></p></div>';
					}
					break;
			}
			
			$tools = '';
			
			if ($row->idmemberto == $this->session->userdata['idmember'])
			{
				$tools .= '<div class="gtool">
								<a href="'.base_url().'message/reply/'.$row->id.'-'.$row->idmemberfrom.'-'.$row->idmemberfromtype.'-4-'.$row->idsubscription.'" rel="facebox">'.
									$this->lang->line('company_respond').
								'</a>
								&middot; <a href="'.base_url().'interview/start/'.$row->idsubscription.'" target="_blank">'.
									$this->lang->line('company_startinterview').
								'</a>
							</div>';
			}
			
			$grid_messages[] = '<div class="row">
								<span>
									<span class="gdesc">'.$message_content.'</span>
								</span>
								'.$tools.'
								</div>';
		}
		
		if (!$grid_messages) {
			 $data['grid_messages'] = '<center>'.$this->lang->line('company_messagesnoresults').'</center>';
		} else {
			 $data['grid_messages'] = implode('<hr/>', $grid_messages) . '<script type="text/javascript">loadFacebox();</script>';
		}
		
		$count_messages = $this->message->get_count_messages();
		
		if ($count_messages > $data['limit'])
		{
			$pagination = '<div class="both"><center> '.$this->lang->line('global_pagination').' <select id="message_offset" class="input" onchange="Company.getMessageGrid();">';
			$count_links = $count_messages / $data['limit'];
			
			for ($i = 1; $i <= $count_links; $i++)
			{
				$pagination .= '<option value="'.($i - 1).'">'.$i.'</option>';
			}
			
			$pagination .= '</select></center></div>';
			
			$pagination .= '<script type="text/javascript">$("#message_offset").val("'.$data['offset'].'");</script>';
			
			$data['grid_messages'] .= '<br/>' . $pagination;
		}
        
        exitHTML($data['grid_messages']);
	}
}

?>